package com.starry.push.netty.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.AbstractConnectionFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * @author WuChao
 * @version 1.0
 * @date 2020/11/9 14:51
 */
@Configuration
public class RabbitConfig {

    @Value("${spring.rabbitmq.host}")
    private String host;

    @Value("${spring.rabbitmq.port}")
    private int port;

    @Value("${spring.rabbitmq.username}")
    private String username;

    @Value("${spring.rabbitmq.password}")
    private String password;

    public static final String ITEM_TOPIC_EXCHANGE = "topic_exchange";
    public static final String MESSAGE_QUEUE = "MessageQueue";
    public static final String MESSAGE_RESULT_QUEUE = "MessageResultQueue";
    public static final String MESSAGE_KEY = "MessageKey";
    public static final String MESSAGE_RESULT_KEY = "MessageResultKey";


    @Bean
    public RabbitAdmin connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host,port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPublisherConfirms(true);
        return new RabbitAdmin(connectionFactory);
    }



    /**
     * 声明交换机
     * @return
     */
    @Bean("TopicExchange")
    public Exchange topicExchange(){
        return ExchangeBuilder.topicExchange(ITEM_TOPIC_EXCHANGE).durable(true).build();
    }

    /**
     * 声明队列
     * @return
     */
    @Bean("MessageQueue")
    public Queue MessageQueue(){
        return QueueBuilder.durable(MESSAGE_QUEUE).build();
    }



    /**
     * 声明队列
     * @return
     */
    @Bean("MessageResultQueue")
    public Queue MessageResultQueue(){
        return QueueBuilder.durable(MESSAGE_RESULT_QUEUE).build();
    }


    /**
     * 将队列绑定到交换机
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding MessageQueueExchange(@Qualifier("MessageQueue") Queue queue,
                                            @Qualifier("TopicExchange")Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(MESSAGE_KEY).noargs();
    }


    /**
     * 将队列绑定到交换机
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding MessageResultQueueExchange(@Qualifier("MessageResultQueue") Queue queue,
                                            @Qualifier("TopicExchange")Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(MESSAGE_RESULT_KEY).noargs();
    }



    /**
     * 创建交换机和队列
     */
    @Bean
    public void createExchangeQueue (){
        connectionFactory().declareExchange(topicExchange());
        connectionFactory().declareQueue(MessageQueue());
    }

}
